package day13.demo1.lock;

/**
 * @Auther:小陈的Java
 * 死锁
 */
public class DeadLock {
    public static void main(String[] args) {
        Object obj1 = new Object();
        Object obj2 = new Object();
        Thread thread1 = new Thread();
        new Thread() {
            @Override
            public void run() {
                synchronized (obj1) {
                    try {
                        System.out.println("线程1开始运行");
                        Thread.sleep(10);
                        System.out.println("线程1：给我obj2,才能释放obj1");
                        synchronized (obj2) {
                            System.out.println("释放线程1");
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        thread1.start();

        new Thread() {
            @Override
            public void run() {
                synchronized (obj2) {
                    try {
                        System.out.println("线程2开始运行");
                        Thread.sleep(10);
                        System.out.println("线程2：给我obj1,才能释放obj2");
                        synchronized (obj1) {
                            System.out.println("释放线程2");
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(thread1.getState());
    }
}

